<?php
/**
 * 操作数据前行为验证
 * 2019.8.3
 */
namespace app\applet\behavior;


use app\share\model\Token;


class Check
{
	public function moduleInit()
	{


		#不验证sign的方法,
        $excludeSign = [
            'applet/notify/shoppayment',
            'applet/notify/orderpayment',
            'applet/notify/supply',
            'applet/notify/transferpayment',
            'applet/notify/shopofflinepayment',
            'applet/notify/gradepayment',
//            'applet/member/login',#登录
//            'applet/test/testToken', #测试签名的接口
        ];

        //验证签名
        if( !in_array($action = $this->getAction(), $excludeSign) )
        {
//             $this->checkSign($input); //验证签名
        }
        //不需要验证登陆token
        $exclude = [
            'applet/member/login',#登录
            'applet/notify/shoppayment',#回调
            'applet/notify/orderpayment',
            'applet/notify/supply',
            'applet/notify/transferpayment',
            'applet/notify/shopofflinepayment',
            'applet/notify/gradepayment',
            'applet/wechat/pay',
            'applet/Goods/listGoodsRebateCate',
            'share/upload/upToken', #测试签名的接口
            'applet/storeapply/industryclass', #测试签名的接口
            'applet/goods/advert',
            'applet/goods/notice',
            'applet/goods/goodsclass',
            'applet/goods/homepagegoods',
            'applet/goods/homepagegoodsstore',
            'applet/storeapply/industryclass',
        ];

        if( !in_array($action = $this->getAction(), $exclude) ){

           //没有在$exclude数组里面的都需要验证登陆,token是否存在
           $TokenCheck = new Token();
           $TokenCheck->checkToken( get_header_param("token") );
        }
	}



    //解析操作形成
    private function getAction()
    {
        return strtolower(request()->module())."/".strtolower(request()->controller())."/".strtolower(request()->action());
    }
    //验证签名,确保接口数据安全和避免重放
    private function checkSign( $input )
    {
        $sign      = trim(get_header_param('sign'));
        $timestamp = trim(get_header_param('timestamp'));
        if(empty($sign))
        {
            returnJson(-1004,'sign不能为空');
        }
        if(empty($timestamp))
        {
            returnJson(-1005,'timestamp不能为空');
        }
        //请求仅在30s有效
        if( abs($timestamp - time()) >=30000 )
        {
            returnJson(-1006,  "当前url请求超时");
        }
        $from_sign = $sign;
        $input['timestamp'] = get_header_param('timestamp');
        $sign = $this->getSign( $input );
        if( $from_sign !== $sign ){
            returnJson(-1007,  "签名验证失败");
        }
    }
    /*
     *  生成sign签名  yuYan123456!@#
     */
    private function getSign( $data )
    {
        foreach ($data as $k => $v) {
            if(null === $v && $v !== 0 ){
                unset($data[$k]);
            }
        }
        foreach ($data as $k => $v)
        {
            $Parameters[$k] = $v;
        }
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, TRUE);
        $String = $String."&key=".config('api_sign_key');
        $result_ = strtoupper( md5( $String ) );
        return $result_;
    }
    //将数组转成uri字符串
    private function formatBizQueryParaMap($paraMap, $urLenCode)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v)
        {
            if($urLenCode)
            {
                $v = urlencode($v);
            }
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar = "";
        if (strlen($buff) > 0)
        {
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }

}